﻿using System;
using Castle.Windsor;
using NCommon.Data;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
using NCommon.Configuration;
using NHibernate;
using NHibernate.Cfg;
using NCommon.ContainerAdapter.CastleWindsor;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NCommon.Data.NHibernate;
using Microsoft.Practices.ServiceLocation;
using CommonServiceLocator.WindsorAdapter;
using MyPetShop.Domain;


namespace MyPetShop.Domain.Spec
{
	/// <summary>
	/// Description of BaseTest.
	/// </summary>
	[TestFixture]
	public abstract class TestBase
	{
		protected IWindsorContainer container = null;
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            var configuration = Fluently.Configure()
               .Database(SQLiteConfiguration.Standard
                          .UsingFile("MyPetshop.db")
                          .ShowSql())
               .Mappings(mappings => mappings.FluentMappings.AddFromAssemblyOf<Item>())
               .ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close"))
               .BuildConfiguration();

            ISessionFactory sessionFactory = configuration.BuildSessionFactory();

            new SchemaExport(configuration).Execute(true, true, false);

            container = new WindsorContainer();

            WindsorServiceLocator serviceLocator = new WindsorServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => serviceLocator);

            var adapter = new WindsorContainerAdapter(container);
            NCommon.Configure.Using(adapter)
                .ConfigureState<DefaultStateConfiguration>()
                .ConfigureData<NHConfiguration>(config => config.WithSessionFactory(() => sessionFactory))
                .ConfigureUnitOfWork<DefaultUnitOfWorkConfiguration>(config => config.AutoCompleteScope());
        }
        
        
	}
}
